/**
 * Created by lby on 2018/6/2.
 */
var mennuInfoData = {};

/**
 * from验证
 */
$().ready(function() {
    var icon = "<i class='fa fa-times-circle'></i> ";
    $("#menuInfoForm").validate({
        rules : {
            name : {
                required : true,
                minlength : 2
            },
            code: {
                required : true,
                minlength : 2
            },
            pcodeName : {
                required : true
            },
            url : {
                required : true
            },
            topic : {
                required : "#newsletter:checked",
                minlength : 2
            },
            agree : "required"
        },
        messages : {
            pcodeName : {
                required : icon + "请选择父级"
            },
            code : {
                required : icon + "请输入菜单编码",
                minlength : icon + "菜单编码必须两个字符以上"
            },
            name : {
                required : icon + "请输入菜单名",
                minlength : icon + "菜单名必须两个字符以上"
            },
            url : {
                required : icon + "请输入菜单URL",
            }
        }
    })
});

$.validator.setDefaults({
    submitHandler : function() {
        fsave();
    }
});

/**
 * 保存提交
 */
function fsave() {
    setUpData();
    $.ajax({
        cache : true,
        type : "POST",
        url : ctxPath +  "module/editModule",
        data : mennuInfoData,
        async : false,
        error : function(request) {
            clearData();
            parent.layer.msg("操作失败");
        },
        success : function(data) {
            clearData();
            if (data.code == 200) {
                parent.layer.msg("操作成功");
                parent.reLoad();
                var index = parent.layer.getFrameIndex(window.name); // 获取窗口索引
                parent.layer.close(index);
            } else {
                parent.layer.msg(data.msg);
            }
        }
    });
}

/**
 * 关闭窗口
 */
function fclose() {
    var index = parent.layer.getFrameIndex(window.name); // 获取窗口索引
    parent.layer.close(index);
}

/**
 * 选择父级模块弹出框
 */
var openParentCode = function(){
    layer.open({
        type:2,
        title:"选择父级模块",
        area : [ '300px', '400px' ],
        content: ctxPath +  "module/pCodeTreeView"
    })
}

/**
 * 选择上级菜单弹出框选择数据显示
 */
function loadPModule( code,name){
    $("#parentcode").val(code);
    $("#pcodeName").val(name);
}
/**
 * 清除数据
 */
function clearData() {
    mennuInfoData = {};
}

/**
 * 设置数据
 */
function setUpData()
{
    set('id');set('name');set('code');set('parentcode');set('ismenu');set('url');set('order');set('icon');
}

/**
 * 添加值
 */
function set(key) {
    mennuInfoData[key] = $("#" + key).val();
}
